package cn.tedu.ivoscm.dictoption.controller;

import cn.tedu.ivoscm.base.response.JsonResult;
import cn.tedu.ivoscm.dictoption.pojo.dto.DictOptionQuery;
import cn.tedu.ivoscm.dictoption.pojo.dto.DictOptionSaveParam;
import cn.tedu.ivoscm.dictoption.pojo.vo.DictOptionVO;
import cn.tedu.ivoscm.dictoption.service.DictOptionService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/dictoption")
@Api(tags = "05.字典项管理")
public class DictOptionController {
    @Autowired
    private DictOptionService dictOptionService;

    @GetMapping("select")
    public JsonResult selectByDictId(DictOptionQuery doq) {
        log.debug("开始处理查询字典项请求，参数：{}", doq);
        List<DictOptionVO> list = dictOptionService.select(doq);
        return JsonResult.ok(list);
    }

    @PostMapping("save")
    public JsonResult save(@RequestBody DictOptionSaveParam dosp) {
        log.debug("开始处理保存字典项请求，参数：{}", dosp);
        dictOptionService.save(dosp);
        return JsonResult.ok();
    }

    @DeleteMapping("delete/{id}")
    public JsonResult delete(@PathVariable Long id) {
        log.debug("开始处理删除字典项请求，参数：{}", id);
        dictOptionService.delete(id);
        return JsonResult.ok();
    }

    // 定义一个根据字典code差该字典下所有字典项的方法
    @GetMapping("selectByDictCode/{code}")
    public JsonResult selectDictOptionsByDictCode(@PathVariable String code) {
        log.debug("开始处理根据字典code差该字典下所有字典项请求，参数：{}", code);
        List<DictOptionVO> list = dictOptionService.selectDictOptionsByDictCode(code);
        return JsonResult.ok(list);
    }

}
